<?php
namespace Frieza\Modules\Decorationtask\Controllers;

use Illuminate\Support\MessageBag;
use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel as pGridDataModel;
use App\Admin\Extensions\Tools\UserButton;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Form;
use Encore\Admin\Widgets\Tab;
use Illuminate\Http\Request;
use Doraemon\model\DecorationTask\TaskGroup;
use Doraemon\model\DecorationTask\Tips;


class TipsController extends Controller
{
    use BaseController;

    const PAGE_SIZE = 20;

    public function __construct()
    {
        parent::__construct();
        Admin::script('$(".pjax-container").attr("id","pjax-container");');
    }

    private function _filterWhere($params){
        if(isset($params['_columns_'])){
            unset($params['_columns_']);
        }
        if(isset($params['_pjax'])){
            unset($params['_pjax']);
        }
        $page = isset($params['page']) ? $params['page'] : 1;
        $pageSize = isset($params['per_page']) ? $params['per_page'] : self::PAGE_SIZE;
        $limit = [($page - 1) * $pageSize, $pageSize];
        unset($params['page']);
        $where = [];
        foreach ($params as $_k => &$_v) {
            if ($_v <> '') {
                $where[$_k] = $_v;
            }
        }
        $data = [
            'LIMIT' => $limit,
        ];

        if (!empty($where)) {
            $data['AND'] = $where;
        }

        return $data;
    }
    //任务列表
    public function index(Request $request,Content $content){

        $request = $request->all();
        $request = self::_filterWhere($request);
        return $content
            ->header('待办提示信息列表')
            ->breadcrumb(
                ['text' => '提示信息列表', 'url' => '/decorationtask/tips']
            )
            ->body(self::listAction($request));
    }

    public function listAction($request = [], $step = 1){

        $tips_infos = Tips::getTipsNormal();
        $gridModel = new pGridDataModel($tips_infos, count($tips_infos), [
            'perPage' => $request['per_page'] ? : $request['LIMIT'][1]
        ]);

        $listGrid = Admin::grid($gridModel, function (Grid $grid) {
            $grid->disableExport();
            $grid->disableFilter();
            $grid->disablePagination();
            $grid->disableRowSelector();

            $grid->column('id');
            $grid->column('tips', '提示信息')->width(250);
            $grid->column('addtime', '添加时间');
            $grid->column('edittime', '修改时间');
            $grid->column('admin_id', '操作人')->display(function(){
                if(!$this->admin_id) {
                    return '暂无';
                }else{
                    return Admin::user()->find($this->admin_id)->name;
                }
            });

        });

        $list = $listGrid->render();
        return $list;
    }

    //提示信息添加页面
    public function edit(Request $request,Content $content, $id = 0)
    {
        $request = $request->all();
        if($id == 0) {
            return $content
                ->header('待办提示信息列表')
                ->breadcrumb(
                    ['text' => '待办提示信息管理管理', 'url' => '/decorationtask/tips'],
                    ['text' => '添加提示信息', 'url' => '/decorationtask/tips/create']
                )
                ->body(self::editAction($request, $id , True));
        } else {
            return $content
                ->header('待办提示信息列表')
                ->breadcrumb(
                    ['text' => '待办提示信息管理管理', 'url' => '/decorationtask/tips'],
                    ['text' => '修改提示信息', 'url' => '/decorationtask/tips/'.'/'.$id.'/edit']
                )
                ->body(self::editAction($request, $id, False));
        }

    }

    public function editAction($request, $id = 0, $isCreate = false)
    {
        if(!$isCreate) {
            $data = Tips::getTipsById($id);
            $form = new Form($data);
            $form->hidden('id');
        } else {
            $data = [];
            $form = new Form($data);
        }

        $form->text('tips', '提示信息')->setWidth(4, 2)->required('不能为空')->help('超过20字以上的字会在app中已...的形式展示');

        $form->action('/admin/decorationtask/tips/save');
        return $form->render();
    }

    //添加、修改提交
    public function editSubmit(Request $request)
    {
        $request = $request->all();
        if(isset($request['id'])) {
            $data = [
                'tips' => $request['tips'],
                'tips_status' => 1,
                'edittime' => date('Y-m-d H:i:s', time()),
                'admin_id' => Admin::user()->id,
            ];

            $rs = Tips::edit($data, $request['id']);
            if($rs) {
                $success = new MessageBag([
                    'title' => '修改成功'
                ]);
                return redirect('/admin/decorationtask/tips')->with(compact('success'));
            } else {
                $error = new MessageBag([
                    'title' => '修改失败'
                ]);
                return redirect('/admin/decorationtask/tips')->with(compact('error'));
            }
        } else {
            $data = [
                'tips' => $request['tips'],
                'tips_status' => 1,
                'addtime' => date('Y-m-d H:i:s', time()),
                'admin_id' => Admin::user()->id
            ];
            $rs = Tips::add($data);
            if($rs) {
                $success = new MessageBag([
                    'title' => '添加成功'
                ]);
                return redirect('/admin/decorationtask/tips')->with(compact('success'));
            } else {
                $error = new MessageBag([
                    'title' => '添加失败'
                ]);
                return redirect('/admin/decorationtask/tips')->with(compact('error'));
            }
        }

    }

    //删除
    public function del($id = 0)
    {
        $tips_info = Tips::getTipsById($id);
       if($tips_info) {
           $data = [
               'tips_status' => Tips::STATUS_DELETE,
               'edittime' => date('Y-m-d H:i:s', time()),
               'admin_id' => Admin::user()->id
           ];

           $rs = Tips::edit($data, $id);
           if($rs) {
               return response()->json([
                   'status' => true,
                   'message' => "操作成功",
               ]);
           } else {
               return response()->json([
                   'status' => false,
                   'message' => "操作失败",
               ]);
           }
       }

    }

}